package org.springboot.szsrcb.controller;

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springboot.szsrcb.entity.User;
import org.springboot.szsrcb.srvice.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class MenuController {
	private static final Logger logger = LoggerFactory.getLogger(MenuController.class);
	@Autowired
	private UserService userService;
	
/*	@RequestMapping("/frank_test")
	public String greeting() {
		
		return "frank_test";
	}
	
	@RequestMapping("/greeting")
	public String greeting(@RequestParam(value = "name", required = false, defaultValue = "World") String name,
			Model model) {
		model.addAttribute("name", name);
		return "greeting";
	}*/
	


	@RequestMapping("/index")
	public String index(HttpSession httpSession, Model model) {
		String login_name=(String) httpSession.getAttribute("login_name"); 
		if(login_name!=null){
			User u = userService.search_a_user(login_name);
			boolean is_leader = userService.is_leader(u.getName());
			//model.addAttribute("is_leader", is_leader);
			model.addAttribute("user_role", u.getRole());
			model.addAttribute("user_name", u.getName());
			//boolean is_req_input=   userService.is_req_input(u.getName());
			//model.addAttribute("is_req_input", is_req_input);
			logger.info("access index page.");
			return "/index";
		}else{
			logger.info("access login page.");
			return "/login";
		}
	}

	@RequestMapping("/req_new")
	public String req_new(HttpSession httpSession) {
		String login_name=(String) httpSession.getAttribute("login_name"); 
		if(login_name!=null){		
			return "req_new";
		}else{
			logger.info("access login page.");
			return "/login";
		}
	}

	@RequestMapping("/req_assign")
	public String req_assign(HttpSession httpSession) {
		String login_name=(String) httpSession.getAttribute("login_name"); 
		if(login_name!=null){	
			logger.info("req_assign访问了req_list");
			// model.addAttribute("name", name);
			return "req_list";
		}else{
			logger.info("access login page.");
			return "/login";
		}
	}


	@RequestMapping("/req_mgmt")
	public String req_mgmt(HttpSession httpSession) {
		String login_name=(String) httpSession.getAttribute("login_name"); 
		if(login_name!=null){	
			logger.info("req_mgmt");
			return "req_list_mgnt";
		}else{
			logger.info("access login page.");
			return "/login";
		}
	}

	/*
	@RequestMapping("/req_modify_detail")
	public String req_modify_detail() {
		logger.info("req_modify_detail");

		// model.addAttribute("name", name);
		return "req_modify_detail";
	}
	*/
	

	@RequestMapping("/req_trace")
	public String req_trace(HttpSession httpSession) {
		String login_name=(String) httpSession.getAttribute("login_name"); 
		if(login_name!=null){	
			logger.info("req_trace");
			return "req_trace";
		}else{
			logger.info("access login page.");
			return "/login";
		}
	}
	
	@RequestMapping("/test_trace")
	public String test_trace(HttpSession httpSession) {
		String login_name=(String) httpSession.getAttribute("login_name"); 
		if(login_name!=null){	
			logger.info("test_trace");
			return "test_trace";
		}else{
			logger.info("access login page.");
			return "/login";
		}
	}
	
	
	@RequestMapping("/req_my")
	public String req_my(HttpSession httpSession,Model model) {
		String login_name=(String) httpSession.getAttribute("login_name"); 
		if(login_name!=null){	
				logger.info("req_my");
				String current_user=(String) httpSession.getAttribute("user");  
				logger.info("current user:"+current_user);
				model.addAttribute("current_user", current_user);
				return "req_my";
		}else{
			logger.info("access login page.");
			return "/login";
		}		
	}
	
	@RequestMapping("/test_my")
	public String test_my(HttpSession httpSession,Model model) {
		String login_name=(String) httpSession.getAttribute("login_name"); 
		if(login_name!=null){
			logger.info("test_my");
			String current_user=(String) httpSession.getAttribute("user");  
			logger.info("current user:"+current_user);
			model.addAttribute("current_user", current_user);
			return "test_my";
		}else{
			logger.info("access login page.");
			return "/login";
		}		
	}



	
	@RequestMapping("/test_info_collect")
	public String test_info_collect() {
		logger.info("test_info_collect");
		return "test_info_collect";
	}

	@RequestMapping("/query_req")
	public String query_req() {
		logger.info("query_req");
		return "query_req";
	}
	
	@RequestMapping("/query_test")
	public String query_test() {
		logger.info("query_test");
		return "query_test";
	}
	
	@RequestMapping("/query_staffer")
	public String query_staffer() {
		logger.info("query_staffer");
		return "query_staffer";
	}

	
	@RequestMapping("/req_modify_detail_test")
	public String req_modify_detail_test() {
		logger.info("req_modify_detail_test");
		return "req_modify_detail_test";
	}
	
	@RequestMapping("/person_info")
	public String person_info(HttpSession httpSession) {
		String login_name=(String) httpSession.getAttribute("login_name"); 
		if(login_name!=null){		
			return "/person_info";
		}else{
			logger.info("access login page.");
			return "/login";
		}
	}
}
